using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CE.Global;

public partial class controls_usrTraveller : System.Web.UI.UserControl
{
    protected Int64 lngTravellerId;
    protected string strTravellerNo;
    protected string strSurName;
    protected string strSal;
    protected DateTime dtDOB;
    protected string strGender;
    protected string strIDType;
    protected string strIDNo;
    protected string strNationality;
    protected string strRelationship;
    protected string strValidationGroup;    
    protected bool bEnabled = true;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            usrDOB.StartYear = General.GetUTCDateTime().Year - 60;
            usrDOB.EndYear = General.GetUTCDateTime().Year;
            txtSurName.Attributes.Add("onblur", "ShowValidationEffect('" + txtSurName.ClientID + "','" + rfvSurName.ClientID + "');");
            ddlIdType.ClientValidationScript = "CheckTraveller" + strTravellerNo + "IdTypePrefix();";
            txtIDNo.ClientValidationScript = "CheckTraveller" + strTravellerNo + "IdTypePrefix();";

            ddlGender.Attributes.Add("onchange", "return CheckSalutation();");
            ddlSalutation.Attributes.Add("onchange", "return CheckSalutation();");
        }
    }

    public bool IsEnabled
    {
        get { return bEnabled; }
        set
        {
            bEnabled = value;
            SetEnabled();
        }
    }

    public string ValidationGroup
    {
        get { return strValidationGroup; }
        set { strValidationGroup = value; }
    }

    public string TravellerNo
    {
        get
        {
            return strTravellerNo;
        }
        set
        {
            strTravellerNo = value;
        }
    }

    public Int64 TravellerId
    {
        get
        {
            return lngTravellerId;
        }
        set
        {
            lngTravellerId = value;
        }
    }

    public string SurName
    {
        get
        {
            return strSurName;
        }
        set
        {
            strSurName = value;
        }
    }

    public string Salutation
    {
        get
        {
            return strSal;
        }
        set
        {
            strSal = value;
        }
    }

    public DateTime DOB
    {
        get
        {
            return dtDOB;
        }
        set
        {
            dtDOB = value;
        }
    }

    public string Gender
    {
        get
        {
            return strGender;
        }
        set
        {
            strGender = value;
        }
    }



    public string IDType
    {
        get
        {
            return strIDType;
        }
        set
        {
            strIDType = value;
        }
    }

    public string IDNo
    {
        get
        {
            return strIDNo;
        }
        set
        {
            strIDNo = value;
        }
    }

    public string Nationality
    {
        get
        {
            return strNationality;
        }
        set
        {
            strNationality = value;
        }
    }

    public string Relationship
    {
        get
        {
            return strRelationship;
        }
        set
        {
            strRelationship = value;
        }
    }
    

    public void SetEnabled()
    {
        ddlGender.IsEnabled = bEnabled;
        ddlSalutation.IsEnabled = bEnabled;
        ddlIdType.IsEnabled = bEnabled;
        ddlNationality.IsEnabled = bEnabled;
        ddlRelationship.IsEnabled = bEnabled;
        txtSurName.Enabled = bEnabled;
        txtIDNo.IsEnabled = bEnabled;
        usrDOB.IsEnabled = bEnabled;
        rfvSurName.Enabled = bEnabled;
    }

    public void IsEnableValidation(bool bValue)
    {
        ddlGender.IsEnableValidation(bValue);
        ddlSalutation.IsEnableValidation(bValue);
        ddlIdType.IsEnableValidation(bValue);
        ddlNationality.IsEnableValidation(bValue);
        ddlRelationship.IsEnableValidation(bValue);
        rfvSurName.Enabled = bValue;
        txtIDNo.IsEnableValidation(bValue);
        usrDOB.IsEnableValidation(bValue);
    }


    protected override void OnPreRender(EventArgs e)
    {
        SetEnabled();

        if (strTravellerNo != null)
        {
            lblTravellerNo.Text = "Traveller " + strTravellerNo;
        }

        if (strValidationGroup != null)
        {
            ddlSalutation.ValidationGroup = strValidationGroup;
            txtIDNo.ValidationGroup = strValidationGroup;
            ddlIdType.ValidationGroup = strValidationGroup;
            ddlGender.ValidationGroup = strValidationGroup;
            ddlNationality.ValidationGroup = strValidationGroup;
            ddlRelationship.ValidationGroup = strValidationGroup;
            rfvSurName.ValidationGroup = strValidationGroup;
            usrDOB.ValidationGroup = strValidationGroup;
            txtIDNo.ValidationGroup = strValidationGroup;

            //ddlSalutation.ErrorMessage = "Choose " + lblDriverNo.Text + "'s salutation.";

            string scriptSal = "function GetTraveller" + strTravellerNo + "Salutation()";
            scriptSal += "{";
            scriptSal += "return document.getElementById('" + ddlSalutation.ClientID + "_ddlSal').value";
            scriptSal += "}";
            Page.ClientScript.RegisterClientScriptBlock(ddlSalutation.GetType(), ddlSalutation.ID.ToString() + strTravellerNo, scriptSal, true);

            //ddlIdType.ErrorMessage = "Choose " + lblDriverNo.Text + "'s ID type.";
            //usrDOB.ErrorMessage = "Choose " + lblDriverNo.Text + "'s date of birth.";
            //txtIDNo.PrefixErrorMessage = "Choose " + lblDriverNo.Text + "'s ID number initial character.";
            //txtIDNo.PostfixErrorMessage = "Choose " + lblDriverNo.Text + "'s ID number last character.";
            //txtIDNo.IdNoErrorMessage = lblDriverNo.Text + "'s ID Number is not valid.";
            //txtIDNo.IdNoLengthErrorMessage = lblDriverNo.Text + "'s ID number must be 7 digits.";
            //txtIDNo.IdNoRequireErrorMessage = "Require " + lblDriverNo.Text + "'s ID number.";

            string scriptIDNumber = "function GetTraveller" + strTravellerNo + "IDNumber()";
            scriptIDNumber += "{";
            scriptIDNumber += "var IdNumber = '';";
            scriptIDNumber += "IdNumber = document.getElementById('" + txtIDNo.ClientID + "_ddlIdPrefix').value;";
            scriptIDNumber += "IdNumber += document.getElementById('" + txtIDNo.ClientID + "_txtIdNumber').value;";
            scriptIDNumber += "IdNumber += document.getElementById('" + txtIDNo.ClientID + "_ddlIdPostfix').value;";
            scriptIDNumber += "return IdNumber;";
            scriptIDNumber += "}";
            Page.ClientScript.RegisterClientScriptBlock(txtIDNo.GetType(), txtIDNo.ID.ToString() + strTravellerNo, scriptIDNumber, true);

            //ddlGender.ErrorMessage = "Choose " + lblTravellerId.Text + "'s gender.";
            string scriptGender = "function GetTraveller" + strTravellerNo + "Gender()";
            scriptGender += "{";
            scriptGender += "return document.getElementById('" + ddlGender.ClientID + "_ddlGender').value";
            scriptGender += "}";
            Page.ClientScript.RegisterClientScriptBlock(ddlGender.GetType(), ddlGender.ID.ToString() + strTravellerNo, scriptGender, true);

            //ddlOccupation.ErrorMessage = "Choose " + lblDriverNo.Text + "'s Occupation.";
            //ddlJobNature.ErrorMessage = "Choose " + lblDriverNo.Text + "'s job nature.";
            //ddlMaritalStatus.ErrorMessage = "Choose " + lblDriverNo.Text + "'s marital status.";
            //rfvSurName.ErrorMessage = "Require " + lblDriverNo.Text + "'s surname.";
            //rfvDrivingYearSg.ErrorMessage = "Choose " + lblDriverNo.Text + "'s years of driving in singapore.";

            string scriptPrefixId = "function GetTraveller" + strTravellerNo + "PrefixID()";
            scriptPrefixId += "{";
            scriptPrefixId += "var prefixId = '" + txtIDNo.ClientID + "_ddlIdPrefix';";
            scriptPrefixId += "return prefixId;";
            scriptPrefixId += "}";
            Page.ClientScript.RegisterClientScriptBlock(txtIDNo.GetType(), txtIDNo.ID.ToString() + "2" + strTravellerNo, scriptPrefixId, true);

            string scriptIdType = "function GetTraveller" + strTravellerNo + "IdTypeID()";
            scriptIdType += "{";
            scriptIdType += "var IdTypeId = '" + ddlIdType.ClientID + "_ddlIdType';";
            scriptIdType += "return IdTypeId;";
            scriptIdType += "}";
            Page.ClientScript.RegisterClientScriptBlock(ddlIdType.GetType(), ddlIdType.ID.ToString() + strTravellerNo, scriptIdType, true);


        }



        if (lngTravellerId != 0)
        {
            lblTravellerId.Text = lngTravellerId.ToString();
        }

        if (strSal != null)
        {
            ddlSalutation.Saluation = strSal;
        }

        if (strSurName != null)
        {
            txtSurName.Text = strSurName;
        }


        if (strGender != null)
        {
            ddlGender.Gender = strGender;
        }

        if (strRelationship != null)
        {
            ddlRelationship.Relation = strRelationship;
        }

        if (strIDType != null)
        {
            ddlIdType.IdType = strIDType;
        }

        if (strIDNo != null && strIDNo.Length == 9)
        {
            strIDNo = strIDNo.Substring(0, 9);
            txtIDNo.Prefix = strIDNo.Substring(0, 1);
            txtIDNo.IDNumber = strIDNo.Substring(1, 7);
            txtIDNo.Postfix = strIDNo.Substring(8, 1);
        }

        if (strNationality != null)
        {
            ddlNationality.Nationality = strNationality;
        }

        if (dtDOB != null)
        {
            //dtDOB = General.GetUTCDateTime().Date;
            usrDOB.Day = dtDOB.Day;
            usrDOB.Month = dtDOB.Month;
            usrDOB.Year = dtDOB.Year;
        }
    }

    public void SetProperty()
    {
        //if (this.Page.IsValid)
        //{
            lngTravellerId = Convert.ToInt64(lblTravellerId.Text == string.Empty ? "0" : lblTravellerId.Text);
            strSal = ddlSalutation.GetSalutation();
            strSurName = txtSurName.Text;
            strGender = ddlGender.GetGender();
            strRelationship = ddlRelationship.GetRelationship();
            dtDOB = usrDOB.GetDate();

            strIDType = ddlIdType.GetIDType();
            strIDNo = txtIDNo.GetIdNumber();

            strNationality = ddlNationality.GetNationality();
        //}
    }

    public void ClearProperty()
    {
        lngTravellerId = 0;
        strSal = "";
        strSurName = "";
        strGender = "" ;
        strRelationship = "";
        dtDOB = General.GetUTCDateTime().Date;

        strIDType = "Select";
        strIDNo = "";

        strNationality = "";
    }

    public void ChangeValidationEffectServerSide(string strValidationGroup)
    {
        if (rfvSurName.Enabled && rfvSurName.ValidationGroup == strValidationGroup)
        {
            rfvSurName.Validate();
            if (rfvSurName.IsValid == false)
            {
                txtSurName.ControlStyle.CssClass = "validationError";
            }
            else
            {
                txtSurName.ControlStyle.CssClass = "";
            }
        }
        else
        {
            txtSurName.ControlStyle.CssClass = "";
        }

        ddlSalutation.ChangeValidationEffectServerSide(strValidationGroup);
        usrDOB.ChangeValidationEffectServerSide(strValidationGroup);
        ddlGender.ChangeValidationEffectServerSide(strValidationGroup);
        ddlIdType.ChangeValidationEffectServerSide(strValidationGroup);
        txtIDNo.ChangeValidationEffectServerSide(strValidationGroup);
        ddlNationality.ChangeValidationEffectServerSide(strValidationGroup);
        ddlRelationship.ChangeValidationEffectServerSide(strValidationGroup);

    }
}
